// pages/leave/leave.js
const util = require('../../utils/util.js')
const {
  request
} = require("../../utils/index.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 1,
        name: "学友留言",
        isActive: true
      },
      {
        id: 2,
        name: "我的留言",
        isActive: false
      },
    ],
    width: "40%",
    leave: {
      isMy: true, //只看自己
      pageNum: "1", //页码
      pageSize: "6" //条数
    },
    leaveInfo: [], //留言数据
    leaveInfoID: [], //留言数据ID
  },

  // details(e){
  //   console.log(e.currentTarget.dataset.lmid)
  //   wx.navigateTo({
  //     url: '../leavedetails/leavedetails?lmId='+e.currentTarget.dataset.lmid,
  //   })
  // },
  // details2(e){
  //   wx.navigateTo({
  //     url: '../leavedetails2/leavedetails2?lmId='+e.currentTarget.dataset.lmid,
  //   })
  // },

  //选项卡切换
  handleClickItem(e) {
    let {
      index
    } = e.detail;
    let {
      tabs
    } = this.data;
    tabs.forEach((val, i) => {
      i === index ? val.isActive = true : val.isActive = false;
      if (i === index) {
        switch (val.id) {
          case 1:
            this.Messread();
            this.initiative();
            break;
          case 2:
            this.initiative();
            break;
        }
      }
    });
    this.setData({
      tabs
    });
  },

  //留言
  async initiative() {
    this.setData({
      leaveInfo: "",
      "leave.pageNum":"1",
      "leave.pageSize":"6",
    })
    console.log(this.data.leaveInfo)
    if (this.data.leaveInfo == "") {
      try {
        let that = this;
        if (this.data.leave.isMy == true) {
          this.data.leave.isMy = false
        } else {
          this.data.leave.isMy = true
        }
        let par = {
          url: "hw/portal/leaving/pageList?token=" + wx.getStorageSync('token'),
          method: "GET",
          data: that.data.leave,
          header: {
            "Content-Type": "application/json"
          }
        }
        let datainfo = await request(par);
        if (datainfo.status == 100) {
          that.setData({
            leaveInfo: datainfo.data.list,
          })
          let leaveInfoID = []
          for (let i = 0; i < this.data.leaveInfo.length; i++) {
            leaveInfoID.push(this.data.leaveInfo[i].lmId)
            this.setData({
              leaveInfoID: leaveInfoID
            })
          }
          if (that.data.leave.isMy == false) {
            this.Messread();
          }
        }
      } catch (error) {
        console.log(error);
      }
    }
  },

  // 已读消息
  async Messread() {
    let that = this
    console.log(this.data.leaveInfo)
    console.log(that.data.leaveInfoID)
    try {
      let that = this;
      let par = {
        url: "hw/portal/leaving/read_msg?token=" + wx.getStorageSync('token') + "&ids=" + that.data.leaveInfoID.toString(),
        method: "POST",
        querys: {
          "ids": that.data.leaveInfoID.toString()
        },
        header: {
          "Content-Type": "application/json"
        }
      }
      let datainfo = await request(par);
      if (datainfo.status == 100) {
        console.log(datainfo)
      }
    } catch (error) {
      console.log(error);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initiative();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    wx.showNavigationBarLoading(); //显示加载
    //模拟加载  1秒
    let that = this;
    if (this.data.leave.pageNum >= this.data.leaveInfo.lastPage) {
      wx.showToast({
        title: '滑倒底了',
        icon: "none"
      })
      wx.hideNavigationBarLoading(); //停止加载
      return;
    }
    this.setData({
      "leave.pageNum": ++this.data.leave.pageNum
    })
    let par = {
      url: "hw/portal/leaving/pageList?token=" + wx.getStorageSync('token'),
      method: "GET",
      data: that.data.leave,
      header: {
        "Content-Type": "application/json"
      },
    }
    let datainfo = await request(par);
    if (datainfo.data != undefined) {
      let lao = that.data.leaveInfo //将老数据存入
      if (datainfo.data.list) {
        let list = [...lao, ...datainfo.data.list] //新老数据拼接
        that.setData({
          leaveInfo: datainfo.data.list, //重新赋值
          "leaveInfo": list
        })
        let leaveInfo = datainfo.data.list
        let leaveInfoID = []
        for (let i = 0; i < leaveInfo.length; i++) {
          leaveInfoID.push(leaveInfo[i].lmId)
        }
        this.setData({
          leaveInfoID: leaveInfoID
        })
        if (this.data.leave.isMy == false) {
          this.Messread();
        }
      }
    } else {
      // 滑倒底了
    }
    wx.hideNavigationBarLoading(); //停止加载
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})